package database;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public abstract class Individual implements Serializable {

	private static final long serialVersionUID = 1329155077603988070L;
	public static final int NONE = 0;
	public static final int PERSONAL = 1;
	public static final int DIVISION = 2;
	public static final int ALL = 3;
	public int read = NONE;
	public int write = NONE;
	public int delete = NONE;
	public int create = NONE;
	public static final int READ = 0;
	public static final int WRITE = 1;
	public static final int DELETE = 2;
	public static final int CREATE = 3;
	protected List<ChangeListener> changeListeners = new ArrayList<ChangeListener>();
	protected Division division;
	protected String name;

	public Individual() {
	}

	public Individual(String name) {
		this.name = name;
	}

	public void addChangeListener(ChangeListener cl) {
		changeListeners.add(cl);
	}

	public String getName() {
		return name;
	}

	public Division getDivision() {
		return division;
	}

	public boolean hasRights(Patient patient, int action) {
		int x = 0;
		switch (action) {
		case WRITE:
			x = write;
			break;
		case READ:
			x = read;
			break;
		case CREATE:
			x = create;
			break;
		case DELETE:
			x = delete;
			break;
		default:
			return false;
		}
		switch (x) {
		case NONE:
			return false;
		case DIVISION:
			if (getDivision() != null && getDivision().contains(patient)) {
				return true;
			}
		case PERSONAL:
			if ((this instanceof Staff && ((Staff) this).getPatients()
					.contains(patient))
					|| (this instanceof Patient && equals(patient))) {
				return true;
			}
			return false;
		case ALL:
			return true;
		}
		return false;
	}

	public abstract RecordList getRecords();

}
